package com.beijiteshop.libcommon.view

import android.content.Context
import android.os.Build
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import com.beijiteshop.libcommon.R
import com.beijiteshop.libcommon.databinding.DialogCommonBinding
import com.beijiteshop.libcommon.ext.databinding.inflateBinding
import com.blankj.utilcode.util.LogUtils

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/26 9:55
 * @Description 通用弹框
 */
class CommonDialog : DialogFragment(){

    private lateinit var mBinding: DialogCommonBinding
    private var mNegativeClickListener: ((View,CommonDialog) -> Unit)? = null
    private var mPositiveClickListener: ((View,CommonDialog) -> Unit)? = null
    private var mTitle: String? = null
    private var mContent: String? = null
    private var mNegativeBtn: String? = null
    private var mPositiveBtn: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NORMAL, R.style.TransBgBottomSheet)
    }

    override fun onStart() {
        super.onStart()
        val dialog = dialog
        if (dialog != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
                val windowManager = requireActivity().getSystemService(Context.WINDOW_SERVICE) as WindowManager
                val metrics = windowManager.currentWindowMetrics
                //宽度设为屏幕宽度的80%
                dialog.window?.setLayout( (metrics.bounds.width() * 0.8).toInt(), ViewGroup.LayoutParams.WRAP_CONTENT)
            }else{
                val metrics = DisplayMetrics()
                requireActivity().display?.getRealMetrics(metrics)
                //宽度设为屏幕宽度的80%
                dialog.window?.setLayout( (metrics.widthPixels * 0.8).toInt(), ViewGroup.LayoutParams.WRAP_CONTENT)
            }
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mBinding = inflateBinding<DialogCommonBinding>(inflater)
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        initEvent()
    }

    private fun initView(){
        mTitle?.let {
            mBinding.commonTitleTv.text = it
        }

        mContent?.let {
            mBinding.commonContentTv.text = it
        }

        mNegativeBtn?.let {
            mBinding.commonNegativeBtn.text = it
        }

        mPositiveBtn?.let {
            mBinding.commonPositiveBtn.text = it
        }
    }

    private fun initEvent(){
        mBinding.commonNegativeBtn.setOnClickListener {
            mNegativeClickListener?.invoke(it,this)
        }

        mBinding.commonPositiveBtn.setOnClickListener {
            mPositiveClickListener?.invoke(it,this)
        }
    }

    fun setTitle(title: String): CommonDialog{
        mTitle = title
        return this
    }

    fun setContent(content: String): CommonDialog{
        mContent = content
        return this
    }

    fun setNegativeButton(text: String?, listener: (View,CommonDialog) -> Unit) : CommonDialog{
        mNegativeBtn = text
        mNegativeClickListener = listener
        return this
    }

    fun setPositiveButton(text: String?, listener: (View,CommonDialog) -> Unit): CommonDialog{
        mPositiveBtn = text
        mPositiveClickListener = listener
        return this
    }

    fun setNegativeButton(listener: (View,CommonDialog) -> Unit): CommonDialog{
       setNegativeButton(null,listener)
        return this
    }

    fun setPositiveButton(listener: (View,CommonDialog) -> Unit): CommonDialog{
        setPositiveButton(null,listener)
        return this
    }
}